package com.zg.message.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.dao.IDao;
import com.zg.common.core.dao.message.MsgInfoDao;
import com.zg.common.core.dao.message.entity.MsgInfo;
import com.zg.common.core.dao.message.entity.MsgTemplate;
import com.zg.common.core.service.PageBaseService;
import com.zg.message.domain.req.MsgInfoVerifyReq;
import com.zg.message.service.MsgInfoService;
import com.zg.message.service.MsgTemplateService;
import com.zg.message.sys.MsgTypeEnum;
import com.zg.message.sys.SendStateEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/5/22 10:47
 */

@Slf4j
@Service
public class MsgInfoServiceImpl extends PageBaseService<MsgInfo,MsgInfoDao> implements MsgInfoService {

//    @Autowired
//    @Qualifier("msgInfoDao")
//    @Override
//    protected void setDao(IDao<MsgInfo> dao) {
//        this.dao = dao;
//    }

    @Autowired
    private MsgTemplateService msgTemplateService;


    public List<MsgInfo> listBySendState(String sendState, String msgType){
        List<MsgTemplate> list =msgTemplateService.listByType(msgType);
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setTemplateIds(list.stream().map(t->t.getId()).collect(Collectors.toList()));
        msgInfo.setSendState(sendState);
        return list(msgInfo);
    }




    public List<MsgInfo> listByReceiver(Long userId){
        LambdaQueryWrapper<MsgInfo> qw = new LambdaQueryWrapper<>();
        qw.eq( MsgInfo::getSendState, SendStateEnum.SUCCESS.getValue());
        qw.eq( MsgInfo::getReceiver, userId);
        qw.isNull(MsgInfo::getReadTime);
        return baseDao.list(qw);
    }

    public void updateRead(){
        LambdaQueryWrapper<MsgInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(MsgInfo::getSendState, SendStateEnum.SUCCESS.getValue());
        qw.isNull(MsgInfo::getReadTime);
        baseDao.update(qw);
    }

    @Override
    protected void preAdd(MsgInfo source) {
        MsgTemplate msgTemplate = msgTemplateService.getById(source.getTemplateId());
        String template = msgTemplate.getContent();
        String strNum = source.getContent();
        if (StrUtil.isBlank(strNum)) {
            int randomNum = 100000 + new Random().nextInt(900000); // 生成 100000-999999 的随机数
            strNum= String.valueOf(randomNum); // 转为字符串
        }

        String replaced = template.replace("{content}",strNum );
        source.setContent(replaced);
        source.setSendState(SendStateEnum.INIT.getValue());
    }

    @Override
    public  Boolean  verifySmsCode(MsgInfoVerifyReq req) {
        MsgInfo source = getById(req.getId());
//        MsgTemplate msgTemplate = msgTemplateService.getById(source.getTemplateId());
//        String template = msgTemplate.getContent();
        Pattern pattern = Pattern.compile("\\d+"); // 匹配连续数字
        Matcher matcher = pattern.matcher(source.getContent());
        if (matcher.find()) {
            String code = matcher.group();
           if(req.getCode().equals(code)){
               return true;
           }
        }
        return false;
    }
}
